﻿
using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Threading.Tasks;

using FreeSql;
using Kalman;
using Newtonsoft.Json.Linq;

namespace QuDao
{
    /// <summary>
    /// 数据表biz_partner_bill关联的业务对象，处理该表相关的业务逻辑
    /// </summary>
    public sealed partial class BillBO : SingletonBase<BillBO>
    {
        readonly IFreeSql fsql;
        public BillBO() : base()
        {
            fsql = ServiceLocator.Instance.GetRequiredService<IFreeSql>();
        }

        /// <summary>
        /// 获取账单记录
        /// </summary>
        /// <param name="id">主键ID的值</param>
        /// <returns></returns>
        public T GetItem<T>(long id)
        {
            if (id <= 0) return default;
            var item = fsql.Select<Bill>().Where(a => a.BillID == id).ToOne<T>();
            return item;
        }

        public Bill GetItem(long id)
        {
            return GetItem<Bill>(id);
        }

        /// <summary>
        /// 获取账单数据列表
        /// </summary>
        /// <returns></returns>
        public List<T> GetList<T>(long[] ids)
        {
            if (ids == null || ids.Length == 0) return new List<T>();
            var list = fsql.Select<Bill>().Where(a => ids.Contains(a.BillID)).ToList<T>();
            return list;
        }

        /// <summary>
        /// 获取用户账单数据列表
        /// </summary>
        /// <param name="uid">用户ID</param>
        /// <param name="pageIndex">页索引，从1开始</param>
        /// <param name="pageSize">每页记录数，默认10</param>
        /// <returns></returns>
        public List<BillDto> GetListOfUser(long uid, DateTime? beginDate, DateTime? endDate, int billType = 0, int pageIndex = 1, int pageSize = 10)
        {
            if (beginDate == null) beginDate = DateTime.Now.AddMonths(-1).Date;
            if (endDate == null) endDate = DateTime.Now.Date;

            var list = fsql.Select<Bill>()
                .Where(a => a.UserID == uid && a.ClearingDate >= beginDate.Value.Date && a.ClearingDate <= endDate.Value.Date)
                .WhereIf(billType > 0, a => a.BillType == billType)
                .OrderByDescending(a => a.BillID)
                .Page(pageIndex, pageSize)
                .ToList<BillDto>();
            return list;
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="req">分页查询请求对象</param>
        public PagingResult<T> Query<T>(PagingRequest req, int tid)
        {
            if (req == null) throw new AppException("查询数据不能为空");

            var select = fsql.Select<Bill>().WhereIf(tid > 0, a => a.TID == tid);
            foreach (var item in req.FilterItems)
            {
                switch (item.Field)
                {
                    case "AreaID":
                        select.Where(a => a.AreaID == int.Parse(item.Value));
                        break;
                    case "UserID":
                        select.Where(a => a.UserID == long.Parse(item.Value));
                        break;
                    case "TrueName":
                        select.Where(a => a.TrueName.StartsWith(item.Value));
                        break;
                    case "Mobile":
                        var ids = fsql.Select<User>().Where(a => a.Mobile.StartsWith(item.Value)).ToList(a => a.UserID).ToArray();
                        select.Where(a => ids.Contains(a.UserID));
                        break;
                    default:
                        break;
                }
            }

            if (!string.IsNullOrEmpty(req.SortExp))
            {
                select.OrderBy(req.SortExp);//自定义排序
            }
            else
            {
                select.OrderByDescending(a => a.BillID);//默认排序
            }

            var sql = select.ToSql();

            var list = select.Count(out var total).Page(req.PageIndex, req.PageSize).ToList<T>();
            return new PagingResult<T>
            {
                PageIndex = req.PageIndex,
                PageSize = req.PageSize,
                Data = list,
                Total = (int)total
            };
        }

        public PagingResult<Bill> Query(PagingRequest req, int tid)
        {
            return Query<Bill>(req, tid);
        }

    }
}
